import pickle;
from PyQt4 import QtGui, QtCore;

class TraceEventsOptions(QtCore.QObject):    
    def __init__(self, tracePane, traceEvents):
        super(TraceEventsOptions, self).__init__();
        self.parent = tracePane.parent;
        self.mainWin = tracePane.parent.mainWin;
        self.dataModel = tracePane.parent.dataContainer.dataModel;
        self.dataView = tracePane.parent.dataContainer.dataView;
        self.graphScene = tracePane.parent.graphScene;
        self.tracePane = tracePane;
        self.trEvents = traceEvents;
        self.createDockContent();
        
    def onStpDelayChanged(self, iVal):
        self.trEvents.updateDelay(iVal);
        
    def onChkShowActs(self, bVal):
        self.trEvents.bShowActs = bVal;
        self.trEvents.updateLbxTrace();
        
    def onChkShowTrans(self, bVal):
        self.trEvents.bShowTrans = bVal;
        self.trEvents.updateLbxTrace();
        
    def onChkTrackStates(self, bVal):
        self.trEvents.bTrackStates = bVal;
        if bVal==False:
            self.trEvents.restoreSceneRect();
            
    def onBtnLoadData(self):
        sTitle = "Load Trace Data...";
        self.sDir = "C:/Users/Owner/workspace/AlgorithmsRRL/src/TraceData/";
        self.sExt = "Trace Data Files (*.dat)";
        self.sFilePath = QtGui.QFileDialog.getOpenFileName(self.mainWin, sTitle, self.sDir, self.sExt);
        if self.sFilePath == "": return;
        dockTitle = self.sFilePath.split("/")[len(self.sFilePath.split("/"))-2];
        dockTitle = "Trace State Events - " + dockTitle;
        self.tracePane.myDockWidget.lblTitle.setText(dockTitle);
        self.loadTraceData();
        
    def loadTraceData(self):
        traceData = pickle.load(open(self.sFilePath));
        self.dataModel.gralMdpParams[2] = len(traceData);
        self.dataModel.nTimeEvents = len(traceData);
        self.dataModel.updateStTraceEvents();
        self.dataModel.stTraceEvents = traceData;
        self.dataView.gridMdpParams.updateGrid();
        self.dataView.gridStTraceEvents.updateGrid();
        sP = str(self.sFilePath);
        sP = sP[0:sP.index(sP.split("/")[len(sP.split("/"))-1])];
        self.trEvents.traceDataPath = sP;        
        
    def createDockContent(self):
        #CheckBoxes & Labels
        self.chkShowActs = QtGui.QCheckBox("Show Actions", self.mainWin);
        self.chkShowTrans = QtGui.QCheckBox("Show Transitions", self.mainWin);
        self.chkTrackStates = QtGui.QCheckBox("Track States", self.mainWin);
        self.btnLoadData = QtGui.QPushButton("Load Trace Data...", self.mainWin);
        self.lblStpDelay = QtGui.QLabel("Steps Delay (ms):", self.mainWin);
        self.spbxStpDelay = QtGui.QSpinBox(self.mainWin);
        self.chkShowActs.setChecked(True);
        self.chkShowTrans.setChecked(True);
        self.spbxStpDelay.setMaximum(60000);
        self.spbxStpDelay.setMinimum(0);
        self.spbxStpDelay.setSingleStep(100);
        self.spbxStpDelay.setValue(self.trEvents.nDelay);
        self.spbxStpDelay.connect(self.spbxStpDelay, QtCore.SIGNAL("valueChanged(int)"), self.onStpDelayChanged);
        self.chkShowActs.connect(self.chkShowActs, QtCore.SIGNAL("toggled(bool)"), self.onChkShowActs);
        self.chkShowTrans.connect(self.chkShowTrans, QtCore.SIGNAL("toggled(bool)"), self.onChkShowTrans);
        self.chkTrackStates.connect(self.chkTrackStates, QtCore.SIGNAL("toggled(bool)"), self.onChkTrackStates);
        self.btnLoadData.connect(self.btnLoadData, QtCore.SIGNAL("clicked()"), self.onBtnLoadData);
        
        self.gridLayout = QtGui.QGridLayout();
        self.gridLayout.addWidget(self.lblStpDelay, 0,0);
        self.gridLayout.addWidget(self.spbxStpDelay, 0,1);
        self.gridLayout.addWidget(self.chkShowActs, 1,0, 1,2);
        self.gridLayout.addWidget(self.chkShowTrans, 2,0, 1,2);
        self.gridLayout.addWidget(self.chkTrackStates, 3,0, 1,2);
        self.gridLayout.addWidget(self.btnLoadData, 4,0, 1,2);
        self.gridLayout.setRowStretch(5,1);
        self.gridLayout.setColumnStretch(1,1);
        self.qWidget = QtGui.QWidget(self.mainWin);
        self.qWidget.setLayout(self.gridLayout);
        
    def getDataSettings(self):
        traceSettings = [];
        traceSettings.append(self.chkShowActs.isChecked());
        traceSettings.append(self.chkShowTrans.isChecked());
        traceSettings.append(self.chkTrackStates.isChecked());
        traceSettings.append(self.spbxStpDelay.value());
        return traceSettings;
    
    def setDataSettings(self, traceSettings):
        self.chkShowActs.setChecked(traceSettings[0]);
        self.chkShowTrans.setChecked(traceSettings[1]);
        self.chkTrackStates.setChecked(traceSettings[2]);
        self.spbxStpDelay.setValue(traceSettings[3]);

